<?php
# Response parsing code.
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_once 'Zend/Http/Response.php';

class RawsResponse
{
  protected $_response = null; 
  protected $_is_parsed = False;
  protected $_do_throw = False;
  protected $_raws_code = null;
  protected $_raws_msg = null;
  protected $_raws_reasons = array();
  protected $_raws_details = array();

  public function __construct($response, $throw_parse_errs = False)
  {
    $this->_response = $response;
    $this->_do_throw = $throw_parse_errs;
    $this->parseRawsErrorBody();
  }

  public function parseRawsErrorBody()
  {
    if ( $this->_response->isError() && (!$this->_is_parsed) ) {
      $body = $this->_response->getBody();
      if ($body) {
        $doc = new DOMDocument();
        $success = $doc->loadXML($body);
        if (!$success) {
          if ($this->_do_throw) {
            require_once 'Zend/Gdata/App/Exception.php';
            throw new Zend_Gdata_App_Exception("Unable to parse response body.");
          }
          $this->_is_parsed = True;
          return;
        }
        $raws_error = $doc->getElementsByTagName("error")->item(0);
         if (!$raws_error) {
           if ($this->_do_throw) {
            require_once 'Zend/Gdata/App/Exception.php';
            throw new Zend_Gdata_App_Exception('No raws:error element found in response body.');
          }
          $this->_is_parsed = True;
          return;
        }
        $code = $raws_error->getElementsByTagName("code");
        if ($code) {
          $this->_raws_code = $code->item(0)->nodeValue;
        }
        $msg = $raws_error->getElementsByTagName("message");
        if ($msg) {
          $this->_raws_msg = $msg->item(0)->nodeValue;
        }
        $reasons = $raws_error->getElementsByTagName("reason");
        if ($reasons) {
          foreach ($reasons as $reason) {
            $this->_raws_reasons[] = $reason->nodeValue;
          }
        }
        $details = $raws_error->getElementsByTagName("details");
        if ($details) {
          $details_elem = $details->item(0);
          if ($details_elem) {
            $children = $details_elem->getElementsByTagName("*");
            foreach ($children as $child) {
              $this->_raws_details[$child->tagname] = $child->nodeValue;
            }
          }
        }
      }
    }
    $this->_is_parsed = True;
  }
  
  public function getCode()
  {
    return $this->_raws_code;
  }

  public function getMsg()
  {
    return $this->_raws_msg;
  }

  public function getReasons()
  {
    return $this->_raws_reasons;
  }

  public function getDetails()
  {
    return $this->_raws_details;
  }
  
}
